<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：IUrl.class.php
 * @类功能: 任务管理
 * @开发者: zc
 * @开发时间： 15-10-22
 * @版本：version 1.0
 */
namespace app\common\ext;

use app\common\cache\common\IAreaCache;
use app\common\cache\user\IUserCache;
use app\common\enum\IsAccumulationFund;
use app\common\enum\IsCar;
use app\common\enum\IsCompany;
use app\common\enum\IsCreditCard;
use app\common\enum\IsHouse;
use app\common\enum\IsPolicy;
use app\common\enum\IsSocial;

class IBid extends ICommon {

    public static function getInfo($where){
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbFiled('user_pool_bid_id,money,period,purpose,user_id,bid_start,bid_end,pool_bid_status,create_time')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::$error = '订单不存在！';
            return false;
        }

        $expire_hour = get_config('OpenBidDate',0);

        $bid_info['bid_start_timestamp'] = strtotime($bid_info['bid_start']);
        $bid_info['bid_end_timestamp'] = strtotime($bid_info['bid_end']);
        $bid_info['bid_expire_timestamp'] = strtotime($bid_info['bid_end']." +{$expire_hour} hour");

        $bid_info['bid_start'] = substr($bid_info['bid_start'],0,10);
        $bid_info['bid_end'] = substr($bid_info['bid_end'],0,10);
        $bid_info['create_time'] = substr($bid_info['create_time'],0,16);

        return $bid_info;
    }

    public static function getList($where,$cover = true){
        $list = IDb::getInstance('user_pool_bid as upb')
            ->setDbFiled('upb.user_pool_bid_id,upb.bid_no,upb.city_id,upb.money,upb.period,upb.bid_num,upb.pool_bid_status,upb.bid_start,upb.bid_end,upb.open_time,upb.stop_time,
                u.occupation,u.is_car,u.is_house,u.is_credit_card,u.is_accumulation_fund,u.is_policy,u.is_social,u.is_company,
                u.real,u.sex')
            ->setDbJoin("user as u","u.user_id=upb.user_id ","left")
            ->setDbWhere($where)
            ->setDbOrder("user_pool_bid_id desc")
            ->setDbLimit(10)
            ->sel();

        //整理数据
        if(!empty($list) && is_array($list) && count($list) > 0){
            $list = self::processList($list,$cover);
        }else{
            $list = [];
        }

        return $list;
    }

    public static function processList($list,$cover = true){
        $expire_hour = get_config('OpenBidDate',0);

        //循环整理数据
        foreach($list as &$data){
            if($cover){
                $data['real'] = get_real_cover($data['real'],$data['sex']);
            }

            $data['city'] = IAreaCache::getName($data['city_id']);
            $data['bid_start_timestamp'] = strtotime($data['bid_start']);
            $data['bid_end_timestamp'] = strtotime($data['bid_end']);
            $data['bid_expire_timestamp'] = strtotime($data['bid_end']." +{$expire_hour} hour");
            $data['bid_end'] = substr($data['bid_end'],0,10);
            $data['open_time'] = substr($data['open_time'],0,10);
            $data['stop_time'] = substr($data['stop_time'],0,10);

            $data['occupation'] = get_enum_name('occupation',$data['occupation']);

            $access_list = [];
            if($data['is_car'] == IsCar::Yes){
                $access_list[] = '有车辆';
            }
            if($data['is_house'] == IsHouse::Yes){
                $access_list[] = '有房产';
            }
            if($data['is_credit_card'] == IsCreditCard::Yes){
                $access_list[] = '有信用卡';
            }
            if($data['is_accumulation_fund'] == IsAccumulationFund::Yes){
                $access_list[] = '有公积金';
            }
            if($data['is_policy'] == IsPolicy::Yes){
                $access_list[] = '有保单';
            }
            if($data['is_social'] == IsSocial::Yes){
                $access_list[] = '有社保';
            }
            if($data['is_company'] == IsCompany::Yes){
                $access_list[] = '有公司';
            }

            $data['access_list'] = $access_list;

            unset($data['sex'],$data['city_id'],$data['bid_start'],$data['is_car'],$data['is_house'],$data['is_credit_card'],$data['is_accumulation_fund'],$data['is_policy'],$data['is_social'],$data['is_company']);
            //设置排序编码
            $data['code'] = '1'.str_pad($data['user_pool_bid_id'],8,0,STR_PAD_LEFT);
        }

        return $list;
    }

    public static function coverPhone($phone){
        return get_string_rsa($phone,3,4);
    }
}